package org.example.job;

import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;

public class JobDemo {
    public static void main(String[] args) {
        /**
         * 启动主方法就加载任务信息
         */
        new JobScheduler(createRegistryCenter(), //第一个参数:注册中心信息,任务信息要放在哪里要告诉elasticjob
                createJobConfiguration()).init();//第二个参数:任务配置  任务在什么时候要执行  要告诉elasticjob
    }

    private static CoordinatorRegistryCenter createRegistryCenter() {
        //配置zk地址,调度任务的组名
        ZookeeperConfiguration zookeeperConfiguration = new ZookeeperConfiguration(
                "localhost:2181", //第一个参数:zookeeper地址
                "elastic-job-demo" //第二个参数:namespace 命名空间
        );
        zookeeperConfiguration.setSessionTimeoutMilliseconds(100);
        CoordinatorRegistryCenter regCenter = new ZookeeperRegistryCenter(zookeeperConfiguration);
        regCenter.init();
        return regCenter;
    }

    private static LiteJobConfiguration createJobConfiguration() {
        //创建作业配置
        // ...
        // 定义作业核心配置
        JobCoreConfiguration simpleCoreConfig = JobCoreConfiguration.newBuilder(
                "demoSimpleJob",//第一个参数:任务名称
                "0/5 * * * * ?",//第二个参数:cron表达式
                1  //第三个参数:分片数量
        ).build();
        // 定义SIMPLE类型配置
        SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(
                simpleCoreConfig,
                MyElasticJob.class.getCanonicalName());
        // 定义Lite作业根配置
        LiteJobConfiguration simpleJobRootConfig = LiteJobConfiguration.newBuilder(simpleJobConfig).build();
        return simpleJobRootConfig;
    }
}